package com.allwees.bs.c.module.ops.repository;

import com.allwees.bs.c.module.base.repository.BaseRepository;
import com.allwees.bs.c.module.ops.entity.CouponDefEntity;
import com.allwees.bs.core.model.coupon.constant.ECouponUseType;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface CouponDefRepository extends BaseRepository<CouponDefEntity, Integer> {

    @Query(value = "select t from CouponDefEntity t where t.useType = :useType and t.status = 0 and t.isDeleted = 0")
    CouponDefEntity findByUseType(@Param("useType") ECouponUseType useType);

    @Query(value = "select t from CouponDefEntity t where t.useType in (:useTypes) and t.status = 0 and t.isDeleted = 0")
    List<CouponDefEntity> findByUseTypes(@Param("useTypes") ECouponUseType... useTypes);

    @Query(value = "select t from CouponDefEntity t where t.isDeleted = 0 and t.status = 0 and t.uuid=:uuid")
    CouponDefEntity getByUuid(@Param("uuid") String uuid);

//    @Query(value = "select t from CouponDefEntity t where t.isDeleted = 0 and t.status = 0 and t.no=:no")
//    CouponDefEntity findByNo(String no);

    @Modifying
    @Query(value = "update coup_coupon_def t set t.received_count=t.received_count + 1 where t.uuid=:uuid",nativeQuery = true)
    int incReceivedCount(String uuid);
}
